﻿using System;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Syngine;
using Syngine.Physics;
using Syngine.Physics.Components;

namespace BenchMark.ShootingGame
{
	public class BlueCircle : DrawableSprite
	{
		private readonly Vector2 _forceConstant = new Vector2(100.0f, 100.0f);
		private Color _color;
		private TimeSpan _timeToLive = TimeSpan.MinValue;

		public BlueCircle() : this(Color.White)
		{

		}

		public BlueCircle(Color color)
		{
			_color = color;
		}

		//private static Texture2D StaticTexture { get; set; }

		public Body CircleBody
		{
			get
			{
				return Body;
			}
		}

		public Vector2 ForceConstant
		{
			get
			{
				return _forceConstant;
			}
		}

		public Vector2 Position
		{
			get
			{
				return Body.Position;
			}
		}

		public bool IsDead { get; private set; }

		protected bool HasCollided { get; set; }

		public override void LoadContent(LoadContext context)
		{
			base.LoadContent(context);

			var game = GameContext.Game;
			var random = Rand.Random;
			var x = random.Next(Texture.Width() / 2, game.GraphicsDevice.Viewport.Width - (Texture.Width() / 2));
			var y = random.Next(Texture.Height() / 2, game.GraphicsDevice.Viewport.Height - (Texture.Height() / 2));
			LoadContent(context, new Vector2(x, y));
		}

		protected override Texture2D GetAsset(LoadContext context)
		{
			return context.Load<Texture2D>("Circle");
		}

		public void LoadContent(LoadContext context, Vector2 position)
		{
			base.LoadContent(context);

			Body = BodyFactory.CreateCircle(WorldContext.World, ConvertUnits.ToSimUnits(Texture.Width() / 2), 1f);
			Body.BodyType = BodyType.Dynamic;
			Body.Position = position.ToSimUnits();
			Body.Restitution = 0.8f;
			Body.Mass = 1.0f;
			Body.OnCollision += (a, b, contact) =>
			{
				if (HasCollided || ((a.Body.UserData == null || b.Body.UserData == null) || (!(bool)a.Body.UserData && !(bool)b.Body.UserData)))
				{
					return true;
				}

				if (!HasCollided && _timeToLive == TimeSpan.MinValue)
				{
					HasCollided = true;
					_color = Color.Red;
					_timeToLive = TimeSpan.FromSeconds(0.25d);
				}

				return true;
			};
		}

		protected override void UpdateCore(UpdateContext context)
		{
			if (_timeToLive > TimeSpan.Zero)
			{
				_timeToLive = _timeToLive - context.GameTime.ElapsedGameTime;
			}
			else if (_timeToLive > TimeSpan.MinValue)
			{
				IsDead = true;
				_timeToLive = TimeSpan.MinValue;
			}

			base.UpdateCore(context);
		}

		public override void UnloadContent()
		{
		}

		//protected override void DrawCore(DrawContext context)
		//{
		//	var bounds = Texture.Bounds;
		//	var x = ConvertUnits.ToDisplayUnits(Body.Position.X);
		//	var y = ConvertUnits.ToDisplayUnits(Body.Position.Y);
		//	context.SpriteBatch.Draw(Texture, new Vector2(x, y), bounds, _color, Body.Rotation, new Vector2(bounds.Width / 2f, bounds.Height / 2f), 1.0f, SpriteEffects.None, 1.0f);
		//}
	}
}